package mysql.ddl.queries;

import mysql.Table;
import mysql.commands.SqlCommand;

import mysql.Connection;
import mysql.types.Types;

import java.sql.ResultSet;
import java.sql.SQLException;

import static mysql.ddl.SqlStrategy.LOAD_FILE;

/**
 * Created by nuno-rafael-figueiredo on 01-07-2014.
 */
public class SelectColumns extends SqlCommand {

    private Table table;

    public SelectColumns(Connection connection, Table table) {
        super(connection, LOAD_FILE);
        this.table = table;
    }

    @Override
    public void execute() {
        try {
            ResultSet resultSet = connection.executeQuery(String.format("describe %s.%s", table.getSchema().getName(), table.getName()));

            while (resultSet.next()) {
                table.createColumn(
                        resultSet.getString(1),
                        Types.valueOf(resultSet.getString(2)),
                        resultSet.getBoolean(3),
                        resultSet.getString(4),
                        resultSet.getString(5),
                        resultSet.getString(6)
                );
            }

            finish();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
